package com.example.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.reggie.common.R;
import com.example.reggie.dto.DishDto;
import com.example.reggie.dto.SetmealDto;
import com.example.reggie.entity.Category;
import com.example.reggie.entity.Dish;
import com.example.reggie.entity.Setmeal;
import com.example.reggie.entity.SetmealDish;
import com.example.reggie.service.CategoryService;
import com.example.reggie.service.DishService;
import com.example.reggie.service.SetmealDishService;
import com.example.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: 老肥！！！
 * @Date: 2022/11/09/11:05
 * @Description: 不想学代码了！！！
 */
@RestController
@RequestMapping("/setmeal")
@Slf4j
public class SetmealController {
    @Autowired
    SetmealDishService setmealDishService;
    @Autowired
    SetmealService setmealService;
    @Autowired
    CategoryService categoryServicel;
    @Autowired
    DishService dishService;
    //新增套餐
    @PostMapping
//    传输的是json数据，需要用requestbody
    @CacheEvict(value = "setmealCache", allEntries = true)
    public R<String> save(@RequestBody SetmealDto setmealDto){
        setmealService.saveWithDish(setmealDto);
        return  R.success("新增套餐成功");
    }
    @GetMapping("/page")
    public R<Page> page(int page, int pageSize,String name){
//        分页构造器对象
        Page<Setmeal> pageInfo = new Page<>(page,pageSize);
        Page<SetmealDto> dtoPage = new Page<>();

        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        // 添加查询条件，根据name进行like模糊查询
        queryWrapper.like(name!= null,Setmeal::getName,name);
//        添加排序条件，根据更新时间降序排列
        queryWrapper.orderByDesc(Setmeal::getUpdateTime);
        setmealService.page(pageInfo,queryWrapper);

//        对象拷贝   records集合不用拷贝，类型不同
        BeanUtils.copyProperties(pageInfo,dtoPage,"records");
//        创建一个SetmalDto中的records
        List<SetmealDto> list = new ArrayList<>();

        for(Setmeal setmeal : pageInfo.getRecords()){
           SetmealDto setmealDto = new SetmealDto();
//           对象拷贝
            BeanUtils.copyProperties(setmeal,setmealDto);
//            分类ID
            Long categoryId = setmeal.getCategoryId();
//            根据分类id查询分类对象
            Category category = categoryServicel.getById(categoryId);
            if(category!=null){
//                分类名称
                String categoryName = category.getName();
                setmealDto.setCategoryName(categoryName);
            }
//            将拷贝过来的setmealDto并且赋值了categoryName的对象放到setmealDtoPage中的records中
            list.add(setmealDto);
        }
//        赋值records
        dtoPage.setRecords(list);
        return  R.success(dtoPage);
    }
//    删除套餐
    @DeleteMapping
    @CacheEvict(value = "setmealCache", allEntries = true)
    public R<String> delete(@RequestParam List<Long> ids){
        setmealService.removeWithDish(ids);
        return R.success("套餐数据删除成功");
    }
//    修改套餐售卖状态
    @PostMapping("/status/{status}")
    public R<String> updateStatus(@PathVariable("status") Integer status,@RequestParam List<Long> ids){
        if (setmealService.batchUpdateStatus(status,ids)){
            return R.success("套餐状态修改成功");
        }else {
            return R.error("套餐状态无法修改");
        }
    }
//    显示套餐
    @GetMapping("/list")
    @Cacheable(value = "setmealCache",key = "#setmeal.categoryId+'_'+#setmeal.status")
    public R<List<Setmeal>> list(Setmeal setmeal){
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(setmeal.getCategoryId() != null,Setmeal::getCategoryId,setmeal.getCategoryId());
        queryWrapper.eq(setmeal.getStatus() != null,Setmeal::getStatus,setmeal.getStatus());
        queryWrapper.orderByDesc(Setmeal::getUpdateTime);
        List<Setmeal> list = setmealService.list(queryWrapper);
        return R.success(list);
    }
    // 根据id查询套餐信息
    @GetMapping("/{id}")
    public R<SetmealDto> getById(@PathVariable Long id){
        SetmealDto setmealDto  = setmealService.getByIdWithDish(id);
        return  R.success(setmealDto);
    }
//    修改套餐信息
    @PutMapping
    @CacheEvict(value = "setmealCache", allEntries = true)
    public R<String>  update (@RequestBody SetmealDto setmealDto){
        setmealService.updateWithDish(setmealDto);
        return  R.success("修改信息成功" );
    }
    //  移动端点击图片查看套餐详情
    @GetMapping("/dish/{id}")
    public R<List<DishDto>> dish(@PathVariable Long id){
        LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SetmealDish::getSetmealId,id);
        List<SetmealDish> list = setmealDishService.list(queryWrapper);
//
//        LambdaQueryWrapper<Dish> queryWrapper1 = new LambdaQueryWrapper<>();
//        ArrayList<Long> arrayList = new ArrayList<>();
//        for (SetmealDish setmealDish : list){
//            Long dishId = setmealDish.getDishId();
//            arrayList.add(dishId);
//        }
//        queryWrapper1.in(Dish::getId,arrayList);
//        List<Dish> dishList = dishService.list(queryWrapper1);
        List<DishDto> dishDtos = list.stream().map((setmealDish) -> {
            DishDto dishDto = new DishDto();
            //其实这个BeanUtils的拷贝是浅拷贝，这里要注意一下
            BeanUtils.copyProperties(setmealDish, dishDto);
            //这里是为了把套餐中的菜品的基本信息填充到dto中，比如菜品描述，菜品图片等菜品的基本信息
            Long dishId = setmealDish.getDishId();
            Dish dish = dishService.getById(dishId);
            BeanUtils.copyProperties(dish, dishDto);

            return dishDto;
        }).collect(Collectors.toList());
        return R.success(dishDtos);
    }
}


